package com.dragon.protoJo.codec;

import com.dragon.protoJo.bean.msg.ProtoMsg;
import com.google.protobuf.MessageLiteOrBuilder;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;

import java.util.List;


public class WebsocketProtobufEncoder extends MessageToMessageEncoder<MessageLiteOrBuilder> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MessageLiteOrBuilder msg, List<Object> out) throws Exception {
        ProtoMsg.Message message = (ProtoMsg.Message) msg;
        WebSocketFrame frame = new BinaryWebSocketFrame(Unpooled.wrappedBuffer(message.toByteArray()));
        out.add(frame);
    }
}
